<template>
  <!-- 根节点 -->
  <div class="login">
    <!-- layout模块从库中获得 -->
    <el-form :model="ruleForm" :rules="rules" ref="loginRef" class="container">
      <el-form-item class="avatar">
        <!-- 头像 -->
        <img src="../assets/snipaste_20190116_171233.png">
      </el-form-item>
      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" prefix-icon="myicon myicon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="ruleForm.password"
          prefix-icon="myicon-key"
          @keyup.enter.native="handleLogin"
          type="password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { login } from '@/api'
export default {
  data () {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin () {
      // 3.2 调用接口之前，先要通过js校验。js校验直接通过ref获取到dom，然后调用validate方法，该方法是封装好的，直接调用。它里面有一个带参数的回调，那个参数是布尔值，为true表示校验通过，为false表示校验失败
      this.$refs.loginRef.validate(isOk => {
        if (isOk) {
          login({ username: this.ruleForm.username, password: this.ruleForm.password })
            // 返回为一个promise值
            .then(res => {
              console.log(res)
              if (res.data.meta.status === 200) {
                localStorage.setItem('token', res.data.data.token)
                localStorage.setItem('username', res.data.data.username)
                this.$router.push({ name: 'home' })
              } else {
                this.$message.error(res.data.meta.msg)
              }
            })
            .catch(err => {
              console.log(err)
            })
          console.log('调用接口去登录')
        } else {
          alert('请正确输入信息后再提交')
          return false
        }
      })
    }
  }
}

</script>
<style lang='scss' scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
      img {
        width: 100%;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
